import java.util.Arrays;

/**
 * @Author: wanqian
 * @Date: 2021/3/29 17:15
 */
public class QuickSort {

    public static int[] arr = {42, 12, 50, 99, 50, 34, 21, 50, 66};


    public static void main(String[] args) {


        long start = System.currentTimeMillis();
        quickSort(arr, 0, arr.length - 1);
        long end = System.currentTimeMillis();
        long consume = (end - start);
        System.out.println("耗时" + consume + "毫秒");
        System.out.println(String.format("快速排序后： %s", Arrays.toString(arr)));
    }

    public static void quickSort(int[] arr, int left, int right) {
        if (left < right) {
            int l = left;
            int r = right;
            int pivot = arr[(left + right) / 2];
            while (l < r) {
                while (arr[l] < pivot) {
                    l++;
                }
                while (arr[r] > pivot) {
                    r--;
                }
                if (l == r) {
                    break;
                }

                int temp = arr[l];
                arr[l] = arr[r];
                arr[r] = temp;

                if (arr[l] == pivot) {
                    r--;
                } else if (arr[r] == pivot) {
                    l++;
                }
            }

            if (l == r) {
                l++;
                r--;
            }

            quickSort(arr, left, r);
            quickSort(arr, l, right);
        }
    }
}
